package com.devframe.web.taglib;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.devframe.sysbase.loader.SysBaseContainer;

/**
 * 
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;SelectTag&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --代码名称翻译--&lt;/b&gt;
 * </pre>
 * <pre>
 * 支持array\list\map
 * 支持默认值
 * </pre>
 * <pre>
 * &lt;b&gt;--样例--&lt;/b&gt;
 *   SelectTag obj = new SelectTag();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class MapTag extends AbstractBaseTag {
	/**
     * serialVersionUID
     */
    private static final long serialVersionUID = 932991928216241306L;

    /**
	 * 选项列表数据，可以是list,iterator,array,map等类型，可以使用request中的名字或者通过${}获取的对象。从其他对象的属性得到的数据需要使用EL表达式获取
	 */
	private Object items = null;

    /**
     * CODE_TYPE:代码类型字典名，一般为代码表名.
     * 用于直接生成代码表选项
     */
    private String codeType = null; 
	/**
	 * 选中值，支持EL表达式
	 */
	private String value = null; // 

    /**
     * 默认值，支持EL表达式
     * 用于当前值为空的情况
     */
    private String defaultValue = null; // 
	/**
	 * 用于从对象中获取值，作为option的value
	 */
	private String valueField;// 

	/**
	 * 用于从对象中获取值，作为option的text
	 */
	private String labelField;// 
	
	/**
	 * result
	 */
	private String result;

	// ////////////////////////////////////////////
	/**
	 * 标签处理逻辑，根据代码显示名称
	 * @return SKIP_BODY
	 */
	public int doStartTag() {
		try {
            if(codeType!=null){
                items=SysBaseContainer.accessCacheDataHandle().getCodeMap(codeType);
            }
			if (items == null || items.equals("")) {
				return SKIP_BODY;
			}
			Object options = items;
			if (items instanceof String) {
			    options = findAttribute((String) items);
			}
			if (options == null) {
				return SKIP_BODY;
			}

			Object selectValue = null;
			if (value != null && !"".equals(value)) {
				selectValue = pageContext.findAttribute(this.value);
				if (selectValue == null) {
					selectValue = value;
				}
			//按默认值处理	
			} else if (defaultValue!=null && !defaultValue.equals("")){
			    selectValue = pageContext.findAttribute(defaultValue);
                if (selectValue == null) {
                    selectValue = defaultValue;
                }
			} else {
			    result = "";
			    return SKIP_BODY;
			}
			if (options instanceof Map) {
				Map map = (Map) options;
				result = map.get(selectValue).toString();
			} else if (options instanceof Iterator || options instanceof Collection) {

				Iterator it = null;
				if (options instanceof Iterator) {
					it = (Iterator) options;
				} else {
					it = ((Collection) options).iterator();
				}
				while (it.hasNext()) {
					Object entry = it.next();
					Object text = "";
					Object key = "";
					if (valueField == null || valueField.equals("")) {
						key = entry.toString();
					} else {
						key = getPropertyValue(valueField, entry);
					}
					if (labelField == null || labelField.equals("")) {
						text = key;
					} else {
						text = getPropertyValue(labelField, entry);
					}
					if (key.equals(selectValue)) {
						result = text.toString();
					}
				}
			} else if (options.getClass().isArray()) {// 数组

				int length = Array.getLength(options);
				for (int j = 0; j < length; j++) {
					Object entry = Array.get(options, j);
					Object text = "";
					Object key = "";
					if (valueField == null || valueField.equals("")) {
						key = entry.toString();
					} else {
						key = getPropertyValue(valueField, entry);
					}
					if (labelField == null || labelField.equals("")) {
						text = key;
					} else {
						text = getPropertyValue(labelField, entry);
					}
					if (key.equals(selectValue)) {
						result = text.toString();
					}
				}
			}
			
			return SKIP_BODY;
		} catch (Exception e) {
			return SKIP_BODY;
		}
	}
    /**
     * doEndTag
     * @return EVAL_PAGE
     * @throws JspException JspException
     */
	public int doEndTag() throws JspException {
		try {
			JspWriter writer = null;
			try {
				writer = this.getPreviousOut();
			} catch (Exception e1) {
				writer = pageContext.getOut();
			}

			try {
			    if(result==null && value!=null){
			        result = value;
			    }
				writer.print(result);
				result = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return EVAL_PAGE;
		} catch (Exception e) {
			return EVAL_PAGE;
		}
	}

	/** 
	 * release
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
	 */
	public void release() {
		super.release();
	}

	public Object getItems() {
		return items;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public void setItems(String items) {
		this.items = items;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getValueField() {
		return valueField;
	}

	public void setValueField(String valueField) {
		this.valueField = valueField;
	}

	public String getLabelField() {
		return labelField;
	}

	public void setLabelField(String labelField) {
		this.labelField = labelField;
	}
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getCodeType() {
        return codeType;
    }
    public void setCodeType(String codeType) {
        this.codeType = codeType;
    }

}
